package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.common.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.behavior.vos.ApArticleBehaviorVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    ApCollectionBehaviorService apCollectionBehaviorService;

    @Autowired
    StringRedisTemplate redisTemplate;


    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        ApArticleBehaviorVo vo = new ApArticleBehaviorVo();
        if (user == null) {
            //未登录 直接将 4种行为 均返回false
            vo.setIslike(false);
            vo.setIsfollow(false);
            vo.setIsunlike(false);
            vo.setIscollection(false);
        }
        //如果登录
        //需要登录
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "请先登录再点赞");
        }
        //查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "行为实体对象不存在");
        }
        //根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId())), ApLikesBehavior.class);
        //判断喜欢行为是否存在
        if (apLikesBehavior != null) {
            vo.setIslike(true);
        }
        //根据行为实体 文章ID查询 是否不喜欢
        //判断不喜欢行为是否存在
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId())), ApUnlikesBehavior.class);
        if (apUnlikesBehavior != null) {
            vo.setIsunlike(true);
        }
        //根据行为实体 文章ID查询 是否收藏
        ApCollection apCollection = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId())), ApCollection.class);
        //判断不喜欢行为是否存在
        if (apCollection != null) {
            vo.setIscollection(true);
        }
        //根据登录用户id 去redis中查询是否关注该作者
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
        if (score!=null){
            vo.setIsfollow(true);
        }
        //封装结果 返回
        return ResponseResult.okResult(vo);
    }
}
